import {
	userInfo
} from "../../api/user.js";
import {
	LOGIN_STATUS,
	USER_INFO,
	WX_USER_INFO,
	USER_ID,
	USER_LOCATION,
	AREA_ID,
	PROVINCE_ID,
	CITY_ID,
	DIAN_PU_LOCATION,
	CITY_NAME,
	LOADED_AREA_ID,
	PARENT_ID,
	RECOMMEND_ID
} from '../../config/cache';
import Cache from '../../utils/cache';

const state = {
	app_token: Cache.get(LOGIN_STATUS) || '',
	userInfo:Cache.get(USER_INFO)||{},
	wxUserInfo:{},
	user_id:Cache.get(USER_ID) || 0,
	homeActive: false,
	userLocation:{},
	area_id:Cache.get(AREA_ID) || '',
	province_id:Cache.get(PROVINCE_ID) || '',
	city_id:Cache.get(CITY_ID) || '', 
	dianpu_location:{
		shipping_name:''
	},
	city_name:Cache.get(CITY_NAME) || '未选择',
	loaded_area_id:Cache.get(LOADED_AREA_ID) || '0',
	parent_id:Cache.get(PARENT_ID) || 0,
	recommend_id:Cache.get(PARENT_ID) || 0,
};

const mutations = {
	LOGIN(state, opt) {
		state.app_token = opt.app_token;
		Cache.set(LOGIN_STATUS, opt.app_token);
	},

	LOGOUT(state) {
		state.app_token = '';
		state.userInfo={}
		state.user_id=0

		Cache.clear(LOGIN_STATUS);
		Cache.clear(USER_INFO)
		Cache.clear(USER_ID)
	},

	UPDATE_USER_ID(state, opt) {
		state.user_id = opt.user_id;
		Cache.set(USER_ID, opt.user_id);
	},
	
	UPDATE_PARENT_ID(state, parent_id) {
		state.user_id = parent_id;
		Cache.set(USER_ID, parent_id);
	},
	
	UPDATE_RECOMMEND_ID(state, recommend_id) {
		state.recommend_id = recommend_id;
		Cache.set(RECOMMEND_ID, recommend_id);
	},

	UPDATE_AREA_ID(state, area_id) {
		state.area_id = area_id;
		Cache.set(AREA_ID, area_id);
	},
	
	
	UPDATE_LOADED_AREA_ID(state, loaded_area_id) {
		state.loaded_area_id = loaded_area_id;
		Cache.set(LOADED_AREA_ID, loaded_area_id);
	},
	
	UPDATE_CITY_NAME(state, name) {
		state.city_name = name;
		Cache.set(CITY_NAME, name);
	},

	UPDATE_PROVINCE_ID(state, province_id) {
		state.province_id = province_id;
		Cache.set(PROVINCE_ID, province_id);
	},

	UPDATE_CITY_ID(state, city_id) {
		state.city_id = city_id;
		Cache.set(CITY_ID, city_id);
	},
	
	UPDATE_PROVINCE_ID(state, province_id) {
		state.province_id = province_id;
		Cache.set(PROVINCE_ID, province_id);
	},

    UPDATE_USER_LOCATION(state, data) {
    	state.userLocation = data;
    	Cache.set(USER_LOCATION, data);
    },

	UPDATE_DIANPU_LOCATION(state, data) {
		state.dianpu_location = data;
		Cache.set(DIAN_PU_LOCATION, data);
	},

	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		state.user_id = userInfo.user_id;
		Cache.set(USER_INFO, userInfo);
		Cache.set(USER_ID, userInfo.user_id);
	},

	UPDATE_WECHATUSERINFO(state, wxUserInfo) {
		state.wxUserInfo = wxUserInfo;
		state.user_id = wxUserInfo.user_id;
		Cache.set(WX_USER_INFO, wxUserInfo);
		Cache.set(USER_ID, wxUserInfo.user_id);
	},
    OPEN_HOME(state) {
    	state.homeActive = true;
    },
    CLOSE_HOME(state) {
    	state.homeActive = false;
    },
};

const actions = {
	USERINFO({
		state,
		commit
	}, force) {
		console.log(force)
		if (state.userInfo !== null && !force)
			return Promise.resolve(state.userInfo);
		else
			return new Promise(reslove => {
				userInfo().then(res => {
					commit("UPDATE_USERINFO", res.data);
					Cache.set(USER_INFO, res.data);
					reslove(res.data);
				});
			}).catch(() => {

			});
	}
};

export default {
	state,
	mutations,
	actions
};
